package org.nanfang.dao.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import org.nanfang.dao.StaffDao;
import org.nanfang.dao.StaffsDaoMap;
import org.nanfang.entity.Staff;
import org.nanfang.util.DruidUtil;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class StaffsDaoimplMap implements StaffsDaoMap {
    QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());
    @Override
    public Map<String, Object> selectById(int id) {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs where id=?";
        MapHandler map = new MapHandler();
        try {
            return qr.query(sql,map,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public List<Map<String, Object>> ListMap() {
        String sql = "select * from staffs";
        MapListHandler mapListHandler = new MapListHandler();
        try {
            return qr.query(sql, mapListHandler);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Staff selectByIdBean(int id) {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs where id=?";
        try {
            return qr.query(sql,new BeanHandler<>(Staff.class),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Staff> ListMapBean() {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs";
        try {
            return qr.query(sql,new BeanListHandler<>(Staff.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String selectByIdColumName(int id) {
        String sql = "select name from staffs where id=?";
        try {
            return qr.query(sql,new ScalarHandler<String>(),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Long selectByIdCount() {
        String sql = "select count(id) from staffs";
        try {
            return qr.query(sql, new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException("查询异常", e);
        }
    }


    @Override
    public List<String> selectAllName() {
        String sql = "select name from staffs";
        try {
            return qr.query(sql, new ColumnListHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException("查询异常", e);
        }
    }

    @Override
    public Object[] selectAllNameObject(int id) {
        String sql = "select id,name,phone,age,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs where id=?";
        try {
            return qr.query(sql, new ArrayHandler(),id);
        } catch (SQLException e) {
            throw new RuntimeException("查询异常", e);
        }
    }

    @Override
    public List<Object[]> selectNameIdList() {
        String sql = "select id,name,phone,age,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs";
        try {
            return qr.query(sql, new ArrayListHandler());
        } catch (SQLException e) {
            throw new RuntimeException("查询异常", e);
        }
    }
}
